package com.loma.common.base

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.loma.common.R
import com.loma.common.databinding.BaseBindingActivityBinding
import com.loma.common.router.PathConfig
import com.loma.common.utils.ext.log
import com.loma.common.utils.ext.showToast
import com.loma.common.utils.ext.singleClick

/***
 * 使用databinding的activity抽像类
 */
abstract class ActivityBase<VM : ViewModelBase, B : ViewDataBinding> : AppCompatActivity() {
    val vm by lazy { ViewModelProvider(this).get(vmClass) }
    abstract val layoutId: Int
    abstract val vmClass: Class<VM>
    lateinit var binding: B
    var progressDialogFragment: ProgressDialogFragment? = null

    lateinit var rootbinding: BaseBindingActivityBinding

    lateinit var rootView: View
    override fun onCreate(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)

        super.onCreate(savedInstanceState)
        rootbinding = DataBindingUtil.setContentView(this, R.layout.base_binding_activity)
        rootbinding.lifecycleOwner = this
        rootbinding.ivBack.singleClick { onBackPressed() }
        rootbinding.tvTitle.text = title
        rootView = initRootView()
        rootbinding.container.addView(rootView)
        initImmersiveStatusBar()
        initView()
        observe()


    }




    open fun observe() {
        //统一显示错误消息
        vm.exception.observe(this, Observer { it ->
            it.localizedMessage?.let {
                showToast(it)
            }

        })

        vm.showProgress.observe(this, Observer {
            if (it) showProgressDialog() else hideProgressDialog()
        })

        vm.submitResult.observe(this, Observer {
            if (it) onOk()
        })


    }




    private fun initRootView(): View {
        binding = DataBindingUtil.inflate(
            layoutInflater,
            layoutId,
            null,
            false
        )
        binding.lifecycleOwner = this
        return binding.root
    }

    open fun initView() {}
    open fun onOk() {}

    fun showProgressDialog() {
        progressDialogFragment = ProgressDialogFragment.Companion.newInstance()
        progressDialogFragment?.show(
            supportFragmentManager,
            R.string.waiting, true
        )
    }

    fun hideProgressDialog() {
        progressDialogFragment?.dismiss()

    }


    /**
     * 设置沉浸式状态栏
     * @param colorResId 状态栏颜色
     */

    protected fun initImmersiveStatusBar() {

        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true)
            .statusBarColor(R.color.white).init()
        rootbinding.toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
    }


    /**
     * 设置沉浸式状态栏
     * @param colorResId 状态栏颜色
     */

    fun initImmersiveStatusBar(color: Int, isDarkFont: Boolean) {
        ImmersionBar.with(this).statusBarDarkFont(isDarkFont).fitsSystemWindows(true)
            .statusBarColor(color).init()
        rootbinding.toolbar.setBackgroundColor(ContextCompat.getColor(this, color))
    }


    fun hideToolbar() {
        rootbinding.toolbar.visibility = View.GONE

    }

    open fun showMenu(text: String, textColor: Int?= null,click: () -> Unit) {
        textColor?.let {
            rootbinding.simpleMenu.setTextColor(ContextCompat.getColor(this,textColor))
        }
        rootbinding.simpleMenu.visibility = View.VISIBLE
        rootbinding.simpleMenu.text = text
        rootbinding.simpleMenu.singleClick {
            click.invoke()

        }

    }
}
